import { BUTTON_STATUS_NUMBER } from '@/config/button'
import { request } from '@/api/service'

export const crudOptions = (vm) => {
  return {

    pageOptions: {
      compact: true
    },
    options: {
      tableType: 'vxe-table',
      rowKey: true, // 必须设置，true or false
      rowId: 'id',
      height: '100%', // 表格高度100%, 使用toolbar必须设置
      highlightCurrentRow: false,
      treeConfig: { // 树形数据配置
        children: 'children',
        hasChild: 'hasChildren',
        expandAll: true
      }
    },
    rowHandle: {
      width: 200,
      view: {
        thin: true,
        text: '',
        disabled () {
          return !vm.hasPermissions('Retrieve')
        }
      },
      edit: {
        thin: true,
        text: '',
        disabled () {
          return !vm.hasPermissions('Update')
        }
      },
      remove: {
        thin: true,
        text: '',
        disabled () {
          return !vm.hasPermissions('Delete')
        }
      },
      custom: [// 自定义按钮
        {
          thin: true,
          text: '终端',
          type: 'warning',
          size: 'small',
          emit: 'terminal',
          // disabled () {
          //   return !vm.hasPermissions('Terminal')
          // },
          show: vm.hasPermissions('Terminal')
        }
      ]
    },

    indexRow: { // 或者直接传true,不显示title，不居中
      title: '序号',
      align: 'center',
      width: 100
    },
    viewOptions: {
      componentType: 'form'
    },
    formOptions: {
      defaultSpan: 24, // 默认的表单 span
      width: '35%'
    },
    columns: [{
      title: '关键词',
      key: 'search',
      show: false,
      disabled: true,
      search: {
        disabled: false
      },
      form: {
        disabled: true,
        component: {
          props: {
            clearable: true
          },
          placeholder: '请输入关键词'
        }
      },
      view: { // 查看对话框组件的单独配置
        disabled: true
      }
    },
    {
      title: 'ID',
      key: 'id',
      show: false,
      disabled: true,
      width: 90,
      form: {
        disabled: true
      }
    },
    {
      title: '设备名称',
      key: 'devices_name',
      sortable: true,
      treeNode: true,
      search: {
        disabled: true,
        component: {
          props: {
            clearable: true
          }
        }
      },
      type: 'input',
      form: {
        // editDisabled: true,
        value: '阿里云ecs',
        rules: [ // 表单校验规则
          { required: true, message: '编码必填项' }
        ],
        component: {
          props: {
            clearable: true
          },
          placeholder: '请输入编码'
        },
        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: '设备型号',
      key: 'devices_type',
      sortable: true,

      search: {
        disabled: true,
        component: {
          props: {
            clearable: true
          }
        }
      },
      type: 'input',
      form: {
        rules: [ // 表单校验规则
          { required: true, message: '型号' }
        ],
        component: {
          props: {
            clearable: true
          },
          placeholder: '请输入设备型号'
        },
        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: '设备用途',
      key: 'devices_usages',
      sortable: true,

      search: {
        disabled: true,
        component: {
          props: {
            clearable: true
          }
        }
      },
      type: 'input',
      form: {
        rules: [ // 表单校验规则
          { required: true, message: '用途' }
        ],
        component: {
          props: {
            clearable: true
          },
          placeholder: '请输入设备用途'
        },
        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: '本地ip',
      key: 'devices_localip',
      sortable: true,

      search: {
        disabled: false,
        component: {
          props: {
            clearable: true
          }
        }
      },

      type: 'input',
      form: {
        // value: this.columns.form.value,
        rules: [ // 表单校验规则
          { required: false, message: '编码' }
        ],
        component: {
          props: {
            clearable: true
          },
          placeholder: '请输入局域网ip，非必填项'
        },
        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: '公网ip',
      key: 'devices_interip',
      sortable: true,

      search: {
        disabled: false,
        component: {
          props: {
            clearable: true
          }
        }
      },

      type: 'input',
      form: {
        // value: this.columns.form.value,
        rules: [ // 表单校验规则
          { required: false, message: '编码' }
        ],
        component: {
          props: {
            clearable: true
          },
          placeholder: '请输入公网ip，非必填项'
        },
        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: '设备编码',
      key: 'devices_num',
      sortable: true,
      show: false,
      search: {
        disabled: false,
        component: {
          props: {
            clearable: true
          }
        }
      },

      type: 'input',
      form: {
        // value: this.columns.form.value,
        rules: [ // 表单校验规则
          { required: false, message: '编码' }
        ],
        component: {
          props: {
            clearable: true
          },
          placeholder: '请输入编码，非必填项'
        },
        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: 'cpu数',
      key: 'devices_cpu',
      sortable: true,

      search: {
        disabled: true,
        component: {
          props: {
            clearable: true
          }
        }
      },

      type: 'input',
      template: '<el-input-number v-model="num" @change="handleChange" :min="1" :max="10" label="描述文字"></el-input-number>',
      form: {
        component: {
          props: {
            clearable: true,
            type: 'input'
          },
          // template: '<el-input-number v-model="num" @change="handleChange" :min="1" :max="10" label="描述文字"></el-input-number>',
          placeholder: 'cpu数量'
        },
        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: '内存',
      key: 'devices_mem',
      sortable: true,

      search: {
        disabled: true,
        component: {
          props: {
            clearable: true
          }
        }
      },

      type: 'input',
      form: {
        component: {
          props: {
            clearable: true,
            type: 'input'
          },
          template: '<el-input-number v-model="num" @change="handleChange" :min="1" :max="10" label="描述文字"></el-input-number>',
          placeholder: '内存大小，单位MB'
        },
        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: '磁盘',
      key: 'devices_disk',
      sortable: true,

      search: {
        disabled: true,
        component: {
          props: {
            clearable: true
          }
        }
      },

      type: 'input',
      form: {
        component: {
          props: {
            clearable: true,
            type: 'input'
          },
          template: '<el-input-number v-model="num" @change="handleChange" :min="1" :max="10" label="描述文字"></el-input-number>',
          placeholder: '磁盘大小，单位GB'
        },
        itemProps: {
          class: { yxtInput: true }
        }
      }
    },
    {
      title: '状态',
      key: 'status',
      sortable: true,
      search: {
        disabled: false
      },

      type: 'radio',
      dict: {
        data: BUTTON_STATUS_NUMBER
      },
      form: {
        value: 1,
        component: {
        }
      }
    },
    {
      title: '用户名',
      key: 'username',
      show: false,
      sortable: true,
      search: {
        disabled: true
      },
      type: 'input'
    },
    {
      title: '端口',
      key: 'port',
      show: false,
      sortable: true,
      search: {
        disabled: true
      },
      type: 'input',
      form: {
        value: 22,
        component: {
        }
      }
    },
    {
      title: '密码',
      key: 'password',
      show: false,
      sortable: true,
      search: {
        disabled: true
      },

      type: 'input',
      form: {
        component: {
          showPassword: true
        }
      }
    },
    {
      title: '设备所属项目',
      key: 'devices_dept',
      show: false,
      search: {
        disabled: true
      },
      type: 'cascader',
      dict: {
        cache: true,
        url: '/api/config/project/?limit=20&page=1',
        isTree: true,
        value: 'id', // 数据字典中value字段的属性名
        label: 'name', // 数据字典中label字段的属性名
        children: 'children', // 数据字典中children字段的属性名
        getData: (url) => { // 配置此参数会覆盖全局的getRemoteDictFunc
          return request({ url: url }).then(ret => {
            // return [{ id: null, name: '根节点', children: XEUtils.toArrayTree(ret.data.data, { parentKey: 'parent', strict: true }) }]
            return ret.data
          })
        }
      },
      form: {
        component: {
          props: {
            elProps: {
              clearable: true,
              showAllLevels: false, // 仅显示最后一级
              props: {
                checkStrictly: true, // 可以不需要选到最后一级
                emitPath: false,
                clearable: true
              }
            }
          }
        },
        helper: {
          render () {
            return (< el-alert title="若部门不存在，则请先创建部门" type="info" />
            )
          }
        }
      }
    }
    ],
    formGroup: {
      type: 'collapse', // tab
      accordion: false,
      groups: {
        base: {
          title: '基本设置',
          icon: 'el-icon-goods',
          columns: ['devices_name', 'devices_usages', 'devices_num', 'devices_type', 'devices_interip', 'devices_localip', 'status', 'devices_ownner']
        },
        logInfo: {
          title: '登录信息',
          icon: 'el-icon-goods',
          columns: ['username', 'port', 'password']
        },
        info: {
          title: '详情',
          collapsed: true,
          icon: 'el-icon-warning-outline',
          columns: ['description', 'dept_belong_id', 'devices_dept', 'devices_type', 'devices_cpu', 'devices_mem', 'devices_disk']
        }
      }
    }
  }
}
